﻿document.onreadystatechange = function () {
    if (document.readyState == "complete") {
        loadValue("#username");
        loadValue("#password");
        loadValue("#interval");
        loadCheckbox("#updateCheck");
    }
};

System.Gadget.onSettingsClosing = function(event) {
    if (event.closeAction == event.Action.commit) {
        saveValue("#username");
        saveValue("#password");
        saveValue("#interval");
        saveCheckbox("#updateCheck");
        event.cancel = false;
    }
};

function saveValue(field) {
    var name = $(field).attr("id");
    var value = $(field).val();
    if (value != "") {
        System.Gadget.Settings.write(name, value);
    }
};

function saveCheckbox(field) {
    var name = $(field).attr("id");
    var value = $(field).attr("checked");
    if (value) {
        System.Gadget.Settings.write(name, true);
    }
    else {
        System.Gadget.Settings.write(name, false);
    }
}

function loadValue(field) {
    var name = $(field).attr("id");
    var value = System.Gadget.Settings.read(name);
    if (value != "") {
        $(field).val(value);
    }
};

function loadCheckbox(field) {
    var name = $(field).attr("id");
    var value = System.Gadget.Settings.read(name);
    if (value == "" || value) {
        $(field).attr("checked", "checked");
    }
    else {
        if ( $(field).attr("checked")) {
            $(field).removeAttr("checked");
        }
    }
    
}